package com.shujia.core

import com.shujia.core.Demo07GroupBy.StuGrp
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo11Filter {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo11Filter")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val stuLineRDD: RDD[String] = sc.textFile("spark/data/students.txt")

    // 将每条数据转换成样例类对象
    val stuRDD: RDD[StuGrp] = stuLineRDD.map(line => {
      val splits: Array[String] = line.split(",")
      StuGrp(splits(0), splits(1), splits(2).toInt, splits(3), splits(4))
    })

    /**
     * filter：主要用于过滤操作
     * 需要返回一个布尔值
     * 返回true则保留数据
     * 返回false则过滤数据
     */

    // 过滤年龄大于23的学生
    stuRDD.filter(_.age > 23).foreach(println)

  }

}
